# -*- coding: utf-8 -*-
# @Time    : 2021/4/22 17:35
# @Author  : mokez
# @Email   : mokez@qq.com
# @File    : 爬取三创图片.py
# @Software: PyCharm

import requests
import re

sc_url = "https://www.sanchuangedu.cn"
# 获取网址
result = requests.get(sc_url)
# print(result.text)
# 打印网站源码文本
msg = result.text
# 使用正则找到<img>标签
# print(re.findall(r'<img src="(.*?)".*>',msg))
ret = re.findall(r'<img src="(.*?)".*>', msg)
# print(ret)

num = 0
# 判断url是否在img标签中的src
# 如果在，则直接用当前i表示img_url
# 如果不在，则加上三创的网站开头url+i
for img_url in ret:
    img_type = img_url.split(".")
    if sc_url in img_url:
        img_url1 = img_url
        # 获取网页上的内容
        sc = requests.get(img_url1)
        with open(f"sc_{num}.{img_type[1]}", "wb") as fp:
            fp.write(sc.content)
        num += 1
    else:
        # print(img_type[1])
        img_url1 = sc_url + img_url
        sc = requests.get(img_url1)
        # print(sc.content)
        # print(sc.text)
        with open(f"sc_{num}.{img_type[1]}", "wb") as fp:
            fp.write(sc.content)
        print(f"下载图片sc_{num}.{img_type[1]}成功！")
        num += 1
